package cn.edu.abtu.listener;

import cn.edu.abtu.dao.EduSubjectDao;
import cn.edu.abtu.entity.EduSubject;
import cn.edu.abtu.entity.excel.SubjectData;
import cn.edu.abtu.exceptionhandler.GuliException;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.Date;
import java.util.UUID;

public class SubjectExcelListener extends AnalysisEventListener<SubjectData> {

    //SubjectExcelListener不能交给Spring管理,需要自己new,不能注入其他对象,不能实现数据库操作
    public EduSubjectDao eduSubjectDao;

    public SubjectExcelListener() {
    }

    public SubjectExcelListener(EduSubjectDao eduSubjectDao) {
        this.eduSubjectDao = eduSubjectDao;
    }

    //读取Excel内容,一行一行的读取
    @Override
    public void invoke(SubjectData subjectData, AnalysisContext analysisContext) {
        if (subjectData == null){
            throw new GuliException(20001,"文件数据为空");
        }
        //判断一级分类是否重复
        EduSubject existOneEduSubject = this.existOneSubject(eduSubjectDao, subjectData.getOneSubjectName());
        if (existOneEduSubject == null){ //没有相同的一级分类,进行添加到数据库
            existOneEduSubject = new EduSubject();
            existOneEduSubject.setParentId("0");
            existOneEduSubject.setTitle(subjectData.getOneSubjectName());
            existOneEduSubject.setGmtCreate(new Date());
            existOneEduSubject.setGmtModified(new Date());
            eduSubjectDao.insert(existOneEduSubject);
        }
        //获取一级分类的ID值
        String parentId = existOneEduSubject.getId();
        System.out.println(parentId);
        //添加二级分类
        //判断二级分类是否重复
        EduSubject existTwoEduSubject = this.existTwoSubject(eduSubjectDao, subjectData.getTwoSubjectName(), parentId);
        if (existTwoEduSubject == null){
            existTwoEduSubject = new EduSubject();
            existTwoEduSubject.setParentId(parentId);
            existTwoEduSubject.setTitle(subjectData.getTwoSubjectName());
            existTwoEduSubject.setGmtCreate(new Date());
            existTwoEduSubject.setGmtModified(new Date());
            eduSubjectDao.insert(existTwoEduSubject);
        }
    }

    //判断一级分类不能重复添加
    private EduSubject existOneSubject(EduSubjectDao eduSubjectDao,String name){
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("title",name);
        queryWrapper.eq("parent_id","0");
        EduSubject oneEduSubject = eduSubjectDao.selectOne(queryWrapper);
        return oneEduSubject;
    }

    //判断二级分类不能重复添加
    private EduSubject existTwoSubject(EduSubjectDao eduSubjectDao,String name,String parentId){
        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("title",name);
        queryWrapper.eq("parent_id",parentId);
        EduSubject twoEduSubject = eduSubjectDao.selectOne(queryWrapper);
        return twoEduSubject;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {

    }
}
